<?php

namespace App\Http\Controllers\Api;

use App\Cart;
use App\Goods;
use App\Http\Controllers\ApiResponse;
use App\Sku;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\JWTAuth;

class CartController extends Controller
{
    protected $uid;
    use ApiResponse;

    /**
     * CartController constructor.
     */
    public function __construct()
    {
        $this->uid = auth('api')->id();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $data = Cart::with(['skus','goods'])->where('uid', $this->uid)->get();
        $total = $data->sum(function ($product) {
            return $product['price'] * $product['number'];
        });
        $result = [
            'total' => $total,
            'list' => $data
        ];
        return $this->data($result);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $sku_id = $request->get('sku_id');
        $goods_info = Goods::find($request->get('goods_id'));
        if (!$goods_info) {
            return $this->message_error('商品已下架或者物品不存在');
        }
        $sku = Sku::find($sku_id);
        if ($sku && $sku['goods_id'] != $goods_info['id']) {
            return $this->message_error("数据错误");
        }
        if ($sku_id) {
            $result = Cart::where([['uid', '=', $this->uid], ['goods_id', '=', $request->get('goods_id')], ['sku_id', '=', $sku_id]])->first();
            if ($result) {
                if ($result->number + $request->get('number') - $sku['stock'] > 0) {
                    return $this->message_error('该类别已售罄');
                }
            } else {
                if ($sku['stock'] - $request->get('number') < 0) {
                    return $this->message_error('该类别已售罄');
                }
            }

        } else {
            $result = Cart::where([['uid', '=', $this->uid], ['goods_id', '=', $request->get('goods_id')]])->first();
            if ($result) {
                if ($result->number + $request->get('number') - $goods_info['stock'] > 0) {
                    return $this->message_error("该类别已售罄");
                }
            } else {
                if ($request->get('number') - $goods_info['stock'] > 0) {
                    return $this->message_error("该类别已售罄");
                }
            }
        }
        if ($result) {
            $result->number = $result->number + $request->get('number');
            $result->save();
        } else {
            $data = [
                'goods_id' => $request->get('goods_id'),
                'goods_name' => $goods_info['name'],
                'number' => $request->get('number'),
                'cover' => $goods_info['cover'],
                'uid' => $this->uid,
                'sku_id' => $sku_id,
                'price' => isset($sku) ? $sku['price'] : $goods_info['price'],
                'type' => $goods_info['type'],
                'origin_price'=>isset($sku) ? $sku['price'] : $goods_info['price'],
            ];
            $result = Cart::create($data);
        }
        return $this->message_success("加入成功");
    }

    /**
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, Request $request)
    {
        $cart = Cart::where('id', $id)->update($request->all());
        return $this->message_success("修改成功");
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        Cart::destroy($id);
        return $this->message_success("删除成功");
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateChecked(Request $request)
    {
        $update = Cart::where('uid', $this->uid)->update(['checked' => $request->get('checked')]);
        if ($update) {
            return $this->message_success("修改成功");
        }
    }
}
